package com.shuzijun.leetcode.editor.cn;
public class ReverseBitsLcci{
    public static void main(String[] args) {
         Solution solution = new ReverseBitsLcci().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int reverseBits(int num) {
        if (num == 0) return 1;
        int[] nums = new int[32];
        for (int i = 0; i < 32; i++) {
            nums[i] = (num&1);
            num >>= 1;
        }

        int[] leftOneCounts = new int[32];
        int count = 0;
        for (int i = 0; i < 32; i++) {
            leftOneCounts[i] = count;
            if (nums[i] == 1){
                count++;
            } else {
                count=0;
            }
        }

        int[] rightOneCounts = new int[32];
        count = 0;
        for (int i = 31; i >= 0; i--) {
            rightOneCounts[i] = count;
            if (nums[i] == 1){
                count++;
            }else {
                count=0;
            }
        }

        int ret = 0;
        for (int i = 0; i < 32; i++) {
            if (ret < leftOneCounts[i] + rightOneCounts[i] + 1){
                ret = leftOneCounts[i] + rightOneCounts[i] +1;
            }
        }

        return ret;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}